package qc.module.sl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.module.sl.dto.station.classify.StationClassifyDto;
import qc.module.sl.dto.station.classify.StationClassifyQueryConditionDto;
import qc.module.sl.entity.SlBasicStationClassify;
import qc.module.sl.mapper.BasicStationClassifyMapper;
import qc.module.sl.repository.SlBasicStationClassifyRepository;

import java.util.List;

/**
 * BasicStationClassifyService
 *
 * @author QuCheng Tech
 * @create 2023/02/14
 */
@Service
public class BasicStationClassifyService {
    private SlBasicStationClassifyRepository repository;
    
    @Autowired
    public void setSlBasicStationClassifyRepository(SlBasicStationClassifyRepository repository){
        this.repository = repository;
    }
    
    /**
     * 根据查询条件查询站点分类信息
     * 
     * @param	dto	站点分类查询dto
     * @return 站点分类信息集合
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    public List<StationClassifyDto> query(StationClassifyQueryConditionDto dto){
        LambdaQueryWrapper<SlBasicStationClassify> wrapper = new LambdaQueryWrapper<>();
        
        //指定查询字段，不需要查询图标相关信息
        wrapper.select(SlBasicStationClassify::getCode, SlBasicStationClassify::getName, SlBasicStationClassify::getDescription, SlBasicStationClassify::getEnable);
        
        if (dto != null){
            if (!StringUtils.isBlank(dto.getStatus()))
                if (dto.getStatus().equals("0") || dto.getStatus().equals("1"))
                    wrapper.eq(SlBasicStationClassify::getEnable, dto.getStatus());
            if (!StringUtils.isBlank(dto.getWords()))
                wrapper.and(k -> k.like(SlBasicStationClassify::getCode, dto.getWords()).or().like(SlBasicStationClassify::getName, dto.getWords()));
        }
        //按分类名称排序
        wrapper.orderByDesc(SlBasicStationClassify::getName);
        
        List<SlBasicStationClassify> list = repository.selectList(wrapper);
        
        if (list.size() > 0x0)
            return BasicStationClassifyMapper.MAPPER.toDtoList(list);
        
        return null;
    }
}
